var reelId = undefined;

num.onchange = function () {
    var num = $('#num').val();
    $('#count').text(num);
};

var checkMateriel = function () {
    var reg = /^[1-9]+.?[0-9]*$/;
    var num = $.trim($('#num').val());
    var materiel = $.trim($('#materiel').val());
    if (materiel === "" || materiel == null) {
        layer.msg('请输入物料号');
        return false;
    }
    if (num <= 0 || !reg.test(num)) {
        layer.msg('请输入大于0的正数');
        return false;
    }
    return true;
};

var checkReel = function () {
    var reg = /^[1-9]+.?[0-9]*$/;
    var num = $.trim($('#num').val());
    var reel = $.trim($('#reel').val());
    if (reel === "" || reel == null) {
        layer.msg('请输入条码');
        return false;
    }
    if (num <= 0 || !reg.test(num)) {
        layer.msg('请输入大于0的正数');
        return false;
    }
    return true;
};

var createPage = function (type) {
    var check;
    switch (type) {
        case 0:
            check = checkMateriel();
            break;
        case 1:
            check = checkReel();
            break;
        default:
            check = false;
            break;
    }
    if (check === false) return;
    var url = "/storage/Tag/insertReelToPurchase";
    var req = {
        reel: reelId,
        count: $('#count').text(),
        materiel: $('#sub_materiel_id').text(),
        supplier: $('#supplierId').val(),
        addContainer: $('#addContainer').prop('checked')
    };
    $.ajax({
        url: url,
        data: req,
        dataType: 'json',
        type: 'post',
        async: false,
        success: function (res) {
            if (res.success === true) {
                console.log('success');
                window.document.body.innerHTML = $('#preview').html();
                window.document.body.innerHTML += "<style> html{display: flex; align-content: center;width: 100%;}>"
                window.print();

                window.location.reload();
            }
            else if (res.success === false) {
                layer.msg(res.message);
            }
        },
        error: function () {
            layer.msg('网络错误');
        }
    });
};

var createId = function (url, data) {
    var success = function (data) {
        if (data.success === true) {
            var reelInfo = data.data;
            reelId = reelInfo['reel_id'];
            delete(reelInfo['reel_id']);
            reelInfo['sub_materiel_name'] = reelInfo['sub_materiel_name'].substr(0, 20);
            reelInfo['format'] = reelInfo['format'].substr(0, 20);
            var company = reelInfo['company_name'];
            var supplier = $('#supplierName').val();
            if (company !== '' && company != null) {
                reelInfo['company_name'] = company.substr(0, 20);
                $('#supplierId').val(reelInfo['supplier_id']);
                console.log($('#supplierName').val());
            } else {
                var supplierName = $("#supplierName").find("option:selected").text();
                reelInfo['company_name'] = supplierName.substr(0, 20);
                $('#supplierId').val(supplier);
                console.log($('#supplierName').val());
            }
            for (var key in reelInfo) {
                $('#' + key).text(reelInfo[key]);
            }
            $("#img").append("<img id='barcode'/>");
            var barcode = document.getElementById('barcode'),
                options = {
                    format: "CODE128",
                    displayValue: true,
                    fontSize: 15,
                    height: 50,
                    width: 1.6
                };
            JsBarcode(barcode, reelId, options);
        } else {
            layer.msg(data.message);
        }
    };
    ajax('get', url, data, success);
};
